package nl.vu.few.ibivu.biomodels.client.ui;

import nl.vu.few.ibivu.biomodels.client.ui.view.GatewayView;
import nl.vu.few.ibivu.biomodels.shared.AuthenticationProvider;
import nl.vu.few.ibivu.biomodels.shared.UserInfo;

import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;

public class GatewayViewImpl extends HLayout implements GatewayView {

	private Presenter presenter;
	
	private ImgButton googleButton;
	private ImgButton facebookButton;
	private ImgButton yahooButton;
	private ImgButton twitterButton;
	private ImgButton logoutButton;
	private Label userInfoLabel;

	public GatewayViewImpl() {
		super();
		this.presenter = null;
		
		this.setMembersMargin(5);
		this.setLayoutMargin(5);
		this.setAutoWidth();
		this.setLayoutAlign(VerticalAlignment.CENTER);
		this.setIsGroup(true);
		
		/*
		 * GOOGLE Button
		 */
		googleButton = new ImgButton();
		googleButton.setSize(32);
		googleButton.setSrc("/images/google-icon.png");
		googleButton.setTooltip("Login using your Google account");
		googleButton.setShowRollOver(false);
		googleButton.setShowDown(false);
		googleButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.login(AuthenticationProvider.GOOGLE);
			}
		});
		googleButton.setVisible(false);
		this.addMember(googleButton);

		/*
		 * FACEBOOK Button
		 */
		facebookButton = new ImgButton();
		facebookButton.setSize(32);
		facebookButton.setSrc("/images/facebook-icon.png");
		facebookButton.setTooltip("Login using your Facebook account");
		facebookButton.setShowRollOver(false);
		facebookButton.setShowDown(false);
		facebookButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.login(AuthenticationProvider.FACEBOOK);
			}
		});
		facebookButton.setVisible(false);
		this.addMember(facebookButton);

		/*
		 * TWITTER Button
		 */
		twitterButton = new ImgButton();
		twitterButton.setSize(32);
		twitterButton.setSrc("/images/twitter-icon.png");
		twitterButton.setTooltip("Login using your Twitter account");
		twitterButton.setShowRollOver(false);
		twitterButton.setShowDown(false);
		twitterButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.login(AuthenticationProvider.TWITTER);
			}
		});
		twitterButton.setVisible(false);
		this.addMember(twitterButton);

		/*
		 * YAHOO Button
		 */
		yahooButton = new ImgButton();
		yahooButton.setSize(32);
		yahooButton.setSrc("/images/yahoo-icon.png");
		yahooButton.setTooltip("Login using your Yahoo! account");
		yahooButton.setShowRollOver(false);
		yahooButton.setShowDown(false);
		yahooButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.login(AuthenticationProvider.YAHOO);
			}
		});
		yahooButton.setVisible(false);
		this.addMember(yahooButton);

		userInfoLabel = new Label();
		userInfoLabel.setVisible(false);
		userInfoLabel.setWrap(false);
		userInfoLabel.setLayoutAlign(VerticalAlignment.CENTER);
		userInfoLabel.setAutoWidth();
		userInfoLabel.setAutoHeight();
		this.addMember(userInfoLabel);

		logoutButton = new ImgButton();
		logoutButton.setSize(24);
		logoutButton.setSrc("/images/logout-icon.png");
		logoutButton.setTooltip("Logout");
		logoutButton.setShowRollOver(false);
		logoutButton.setShowDown(false);
		logoutButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.logout();
			}
		});
		logoutButton.setVisible(false);
		this.addMember(logoutButton);
	}
	
	@Override
	public void setUserInfo(UserInfo userInfo) {
		if (userInfo != null && userInfo.isLoggedIn()) {
			//setIsGroup(false);
			setGroupTitle("");
			userInfoLabel.setContents(userInfo.getProfile().getUser().getNickname() + 
					" (" + userInfo.getProfile().getUser().getEmail() + ")");
			userInfoLabel.setVisible(true);
			googleButton.setVisible(false);
			facebookButton.setVisible(false);
			twitterButton.setVisible(false);
			yahooButton.setVisible(false);
			logoutButton.setVisible(true);
		} else {
			//setIsGroup(true);
			setGroupTitle("Login using...");
			userInfoLabel.setVisible(false);
			googleButton.setVisible(true);
			facebookButton.setVisible(true);
			twitterButton.setVisible(true);
			yahooButton.setVisible(true);
			logoutButton.setVisible(false);
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
}
